package com.popeye.demo.angular.service.controller;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Map;

@RestController
@RequestMapping("demo")
public class DemoServiceController {

    @GetMapping("/get")
    public Map<String, String> get(@RequestParam Map<String, String> params) {
        final ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        final String userCode = attributes.getRequest().getHeader("Login-Name");
        final String accessToken = attributes.getRequest().getHeader("Access-Token");
        params.put("message", "\"GET\" from restful service: " + userCode + "@" + attributes.getRequest().getHeader("Client-Host"));
        return params;
    }

    @PostMapping("/post")
    public Map<String, String> post(@RequestBody Map<String, String> params) {
        final ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        final String userCode = attributes.getRequest().getHeader("Login-Name");
        final String accessToken = attributes.getRequest().getHeader("Access-Token");
        params.put("message", "\"POST\" from restful service: " + userCode + "@" + attributes.getRequest().getHeader("Client-Host"));
        return params;
    }

}
